package com.vnp.vchat.version2;

import com.vnp.vchat.CaidatActivity;
import com.vnp.vchat.MainActivity;
import com.vnp.vchat.MainPanelActivity;
import com.vnp.vchat.R;
import com.vnp.vchat.R.layout;
import com.vnp.vchat.Util.Common;
import com.vnp.vchat.Util.PreferenceHelper;
import com.vnp.vchat.Util.Utils;
import com.vnp.vchat.database.DBAdapter;
import com.vnp.vchat.database.DBFunction;
import com.vnp.vchat.database.MySQLiteHelper;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ChungActivity extends Activity implements OnClickListener{

	ImageButton btnBack;
	TextView title, titleThoatTk, titleXoaTk;
	
	RelativeLayout rlThoat, rlXoa;
	
	Dialog dThoat;
	
	MySQLiteHelper db;
	PreferenceHelper preferenceHelper;
	
	Runnable r = new Runnable() {
		
		@Override
		public void run() {
			gotoLogin();
		}
	};
	
	Handler mHandler = new Handler();
	DBFunction dBF;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chung);
		
		preferenceHelper = new PreferenceHelper(this);
		db = new MySQLiteHelper(this);
		
		dBF = new DBFunction(DBAdapter.getObj(ChungActivity.this));
		
		btnBack = (ImageButton) findViewById(R.id.btn_function1);
		btnBack.setVisibility(View.VISIBLE);
		
		title = (TextView) findViewById(R.id.title);
		titleThoatTk = (TextView) findViewById(R.id.title_thoat_tk);
		titleXoaTk = (TextView) findViewById(R.id.title_xoa_tk);
		
		rlThoat = (RelativeLayout) findViewById(R.id.rl_thoat_tk);
		rlXoa = (RelativeLayout) findViewById(R.id.rl_xoa_tk);
		
		init();
		setFont();
	}
	
	
	void init(){
		title.setText("Chung");
		btnBack.setOnClickListener(this);
		
		rlXoa.setOnClickListener(this);
		rlThoat.setOnClickListener(this);
		
	}
	
	void setFont(){
		
		title.setTypeface(Utils.getTypefaceBold(ChungActivity.this));
		titleThoatTk.setTypeface(Utils.getTypefaceRegular(ChungActivity.this));
		titleXoaTk.setTypeface(Utils.getTypefaceRegular(ChungActivity.this));
		
	}

	@Override
	public void onBackPressed() {
		closeActivity();
	}
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.over_main_right, R.anim.trans_right_out);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_function1:
			closeActivity();
			break;

		case R.id.rl_thoat_tk:
			showDialogThoat();
			break;
		case R.id.rl_xoa_tk:
			break;
			
		case R.id.btn_thoat_tk:
			dThoat.dismiss();
			mHandler.removeCallbacks(r);
			mHandler.postDelayed(r, 300);
			break;
		case R.id.btn_ko:
			dThoat.dismiss();
			break;
		}
	}
	
	void showDialogThoat(){
		dThoat = new Dialog(this);
		dThoat.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dThoat.setContentView(R.layout.dialog_thoat_tk);
		
		
		TextView txtThoat = (TextView) dThoat.findViewById(R.id.str_canhbao_thoat);
		
		Button btnThoat = (Button) dThoat.findViewById(R.id.btn_thoat_tk);
		Button btnKo = (Button) dThoat.findViewById(R.id.btn_ko);
		dThoat.show();
		dThoat.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dThoat.setCanceledOnTouchOutside(true);
		dThoat.getWindow().setGravity(Gravity.CENTER);
		dThoat.show();
		
		txtThoat.setTypeface(Utils.getTypefaceRegular(ChungActivity.this));
		btnThoat.setTypeface(Utils.getTypefaceRegular(ChungActivity.this));
		btnKo.setTypeface(Utils.getTypefaceRegular(ChungActivity.this));
		// setonclick
		btnThoat.setOnClickListener(this);
		btnKo.setOnClickListener(this);
	}
	
	private void gotoLogin(){
		xoaCookies();
		MainPanelActivity.isExit = false;
		
		
		Intent broadcastIntent = new Intent();
		broadcastIntent.setAction("THOAT");
		sendBroadcast(broadcastIntent);
		
		Intent c = new Intent(ChungActivity.this,SplashScreenActivity.class);
		finish();
		c.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(c);
	}
	
	private void xoaCookies(){
//		MainActivity.isExit = false;
		Log.i("DATA", "DA nhay vao xoa cookies");
		 CookieSyncManager.createInstance(this); 
		 CookieManager cookieManager = CookieManager.getInstance();
		 cookieManager.removeAllCookie();
		 cookieManager.removeExpiredCookie();
		 cookieManager.removeSessionCookie();
		 preferenceHelper.setUserId("");
		 preferenceHelper.setDataJoin("");
		 preferenceHelper.setAvatarKH("");
		 preferenceHelper.setDeviceId("");
		 preferenceHelper.setMyAvatar("");
		 preferenceHelper.setUserName("");
		 preferenceHelper.setSettingFeed("");
		 preferenceHelper.setSettingDisplay("");
		 preferenceHelper.setSettingNotifi("");
		 preferenceHelper.setDomainConnect("");
		 preferenceHelper.setPostConnect("");
		 preferenceHelper.setHashSetting("");
		 preferenceHelper.setTimeGetContact(0);
		 preferenceHelper.setSettingsRingtone(1);
		 db.deleteAllKH();
		 db.deleteHisChat();
		 dBF.deleteContactSv();
//		 unregisterGCM();
	}
}
